<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com>
// +----------------------------------------------------------------------

namespace app\admin\controller;

use app\common\controller\Admin;
use app\admin\controller\api\Rule as RuleApi;
/**
 * 权限管理
 * @package app\rule
 */
class Rule  extends Admin

{

	protected $api;

	public function _initialize()
	{
		parent::_initialize();
		$this->api = new RuleApi();
	}
	
	/**
	 * 首页
	 * @return View
	 */
	public function index() {
		if(IS_AJAX){
			return $this->api->ruleList();
		}
		return $this->fetch();
	}
	
	/**
	 * 查询
	 * @return Json
	 */
	public function search() {
		if(IS_AJAX){
			$ruleList = $this->api->search(input('keyword'));
			
			return $ruleList;
		}
	}
	
	/**
	 * 新增规则
	 * @return View
	 */
	public function add(){
		if(IS_POST){
			$data = input('post.');
			if(isset($data['ismenu'])){
				$data['ismenu'] = 1;
			}else{
				$data['ismenu'] = 0;
			}
			if(isset($data['status'])){
				$data['status'] = 1;
			}else{
				$data['status'] = 0;
			}
			return  $this->api->add($data);
			
		}
		$ruleList = $this->api->ruleList()->getData();
		$ruledata = [0 =>'无'];
		foreach ($ruleList['data'] as $k => $v)
		{
			$ruledata[$v['id']] = $v['title'];
		}

		$this->view->assign('ruledata', $ruledata);
		return $this->fetch();
	}
	
	/**
	 * 编辑规则
	 * @param string $ruleId 规则ID
	 * @return View
	 */
	public function edit($ruleId = NULL){
		if(IS_POST){
			$data = input('post.');
			if(isset($data['ismenu'])){
				$data['ismenu'] = 1;
			}else{
				$data['ismenu'] = 0;
			}
			if(isset($data['status'])){
				$data['status'] = 1;
			}else{
				$data['status'] = 0;
			}
			return $this->api->edit($data,$ruleId);
		}
		$ruleList = $this->api->ruleList()->getData();
		$ruledata = [0 =>'无'];
		foreach ($ruleList['data'] as $k => $v)
		{
			$ruledata[$v['id']] = $v['title'];
		}
		//获取父级下拉菜单数据
		$this->view->assign('ruledata', $ruledata);
		//获取菜单信息
		$ruleInfo = model('Rule')->find($ruleId);
     	$this->assign('info',$ruleInfo);
		return $this->fetch();
	}

	/**
	 * 删除规则
	 * return Json
	 */
	public function del(){
		if(IS_GET){
			return $this->api->del();
		}
	}

}